package com.tfswx.jcyw.ruleenginedesigner.service.impl;

import com.tfswx.jcyw.ruleenginedesigner.dao.GzyqXtBdsMapper;
import com.tfswx.jcyw.ruleenginedesigner.dao.GzyqXtGzdyKzMapper;
import com.tfswx.jcyw.ruleenginedesigner.dao.GzyqXtGzdyMapper;
import com.tfswx.jcyw.ruleenginedesigner.dao.model.GzyqXtBds;
import com.tfswx.jcyw.ruleenginedesigner.dao.model.GzyqXtGzdy;
import com.tfswx.jcyw.ruleenginedesigner.dao.model.GzyqXtGzdyKz;
import com.tfswx.jcyw.ruleenginedesigner.dto.gzgl.*;
import com.tfswx.jcyw.ruleenginedesigner.service.GzglService;
import com.tfswx.jcyw.ruleenginedesigner.util.UserManger;
import com.tfswx.ruleengine.compute.RuleEngine;
import com.tfswx.ruleengine.compute.bean.FormDataObject;
import com.tfswx.ruleengine.compute.bean.GzDataDTO;
import com.tfswx.ruleengine.compute.bean.Gzjyjg;
import com.tfswx.ruleengine.compute.bean.RuleInfo;
import com.tfswx.ruleengine.compute.exception.AppException;
import com.tfswx.ruleengine.compute.util.CommonUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author Ricky
 * @date 2021/5/11
 */
@Service
public class GzglServiceImpl implements GzglService {

    @Resource
    private GzyqXtGzdyMapper gzyqXtGzdyMapper;
    @Resource
    private GzyqXtBdsMapper gzyqXtBdsMapper;
    @Resource
    private GzyqXtGzdyKzMapper gzdyKzMapper;

    @Override
    public List<GzdyListByGzflbmQueryResult> queryGzdyListByGzflbm(GzdyListByGzflbmQueryParam queryParam) {
        UserManger.UserInfo loginUserInfo = UserManger.getLoginUserInfo();
        queryParam.setRybm(loginUserInfo.getRybm());
        return gzyqXtGzdyMapper.queryGzdyListByGzflbm(queryParam);
    }

    @Override
    public String addGzdy(GzdyAddInputDTO gzdyAddInputDTO) {
        GzyqXtGzdy gzyqXtGzdy = new GzyqXtGzdy();
        BeanUtils.copyProperties(gzdyAddInputDTO, gzyqXtGzdy);
        gzyqXtGzdy.setGzbm(CommonUtil.getUuid());
        gzyqXtGzdy.setSfqy("Y");
        gzyqXtGzdy.setSfsc("N");
        gzyqXtGzdy.setPx(System.currentTimeMillis());
        gzyqXtGzdy.setCjsj(new Date());
        gzyqXtGzdy.setZhxgsj(new Date());
        int i = gzyqXtGzdyMapper.insert(gzyqXtGzdy);
        if (i != 1) {
            throw new AppException("增加规则定义失败");
        }
        // 插入规则定义扩展表
        UserManger.UserInfo loginUserInfo = UserManger.getLoginUserInfo();
        GzyqXtGzdyKz gzyqXtGzdyKz = new GzyqXtGzdyKz();
        gzyqXtGzdyKz.setCjrybm(loginUserInfo.getRybm());
        gzyqXtGzdyKz.setCjrymc(loginUserInfo.getMc());
        gzyqXtGzdyKz.setGzbm(gzyqXtGzdy.getGzbm());
        gzyqXtGzdyKz.setSfsc("N");
        gzyqXtGzdyKz.setCjsj(new Date());
        gzyqXtGzdyKz.setZhxgsj(new Date());
        i = gzdyKzMapper.insert(gzyqXtGzdyKz);
        if (i != 1) {
            throw new AppException("增加规则定义扩展失败");
        }
        return gzyqXtGzdy.getGzbm();
    }

    @Override
    public boolean updateGzdy(GzdyUpdateInputDTO gzdyUpdateInputDTO) {
        GzyqXtGzdy gzyqXtGzdy = new GzyqXtGzdy();
        BeanUtils.copyProperties(gzdyUpdateInputDTO, gzyqXtGzdy);
        gzyqXtGzdy.setZhxgsj(new Date());
        int i = gzyqXtGzdyMapper.update(gzyqXtGzdy);
        if (i != 1) {
            throw new AppException("修改规则定义失败");
        }
        return true;
    }

    @Override
    public boolean updateGzbds(GzbdsUpdateInputDTO gzbdsUpdateInputDTO) {
        GzyqXtGzdy gzyqXtGzdy = new GzyqXtGzdy();
        BeanUtils.copyProperties(gzbdsUpdateInputDTO, gzyqXtGzdy);
        gzyqXtGzdy.setZhxgsj(new Date());
        int i = gzyqXtGzdyMapper.updateGzbds(gzyqXtGzdy);
        if (i != 1) {
            throw new AppException("修改规则表达式失败");
        }
        return true;
    }

    @Override
    public boolean setGzdyQyzt(GzdyQyztSetInputDTO gzdyQyztSetInputDTO) {
        GzyqXtGzdy gzyqXtGzdy = new GzyqXtGzdy();
        BeanUtils.copyProperties(gzdyQyztSetInputDTO, gzyqXtGzdy);
        gzyqXtGzdy.setZhxgsj(new Date());
        int i = gzyqXtGzdyMapper.setGzdyQyzt(gzyqXtGzdy);
        if (i != 1) {
            throw new AppException("设置规则定义状态失败");
        }
        return true;
    }

    @Override
    public boolean deleteGzdy(GzdyDeleteInputDTO gzdyDeleteInputDTO) {
        GzyqXtGzdy gzyqXtGzdy = new GzyqXtGzdy();
        BeanUtils.copyProperties(gzdyDeleteInputDTO, gzyqXtGzdy);
        gzyqXtGzdy.setZhxgsj(new Date());
        int i = gzyqXtGzdyMapper.delete(gzyqXtGzdy);
        if (i != 1) {
            throw new AppException("删除规则定义失败");
        }
        return true;
    }

    @Override
    public List<BdsListByGzbmQueryOutputDTO> queryBdsListByGzbm(BdsListByGzbmQueryInputDTO bdsListByGzbmQueryInputDTO) {
        GzyqXtBds gzyqXtBds = new GzyqXtBds();
        BeanUtils.copyProperties(bdsListByGzbmQueryInputDTO, gzyqXtBds);
        return gzyqXtBdsMapper.queryBdsListByGzbm(gzyqXtBds);
    }

    @Override
    public String addBds(BdsAddInputDTO bdsAddInputDTO) {
        GzyqXtBds gzyqXtBds = new GzyqXtBds();
        BeanUtils.copyProperties(bdsAddInputDTO, gzyqXtBds);
        gzyqXtBds.setBdsbm(CommonUtil.getUuid());
        gzyqXtBds.setSfsc("N");
        gzyqXtBds.setCjsj(new Date());
        gzyqXtBds.setZhxgsj(new Date());
        int i = gzyqXtBdsMapper.insert(gzyqXtBds);
        if (i != 1) {
            throw new AppException("新增表达式失败");
        }
        return gzyqXtBds.getBdsbm();
    }

    @Override
    public boolean updateBds(BdsUpdateInputDTO bdsUpdateInputDTO) {
        GzyqXtBds gzyqXtBds = new GzyqXtBds();
        BeanUtils.copyProperties(bdsUpdateInputDTO, gzyqXtBds);
        gzyqXtBds.setZhxgsj(new Date());
        int i = gzyqXtBdsMapper.update(gzyqXtBds);
        if (i != 1) {
            throw new AppException("编辑表达式失败");
        }
        return true;
    }

    @Override
    public boolean deleteBds(BdsDeleteInputDTO bdsDeleteInputDTO) {
        GzyqXtBds gzyqXtBds = new GzyqXtBds();
        BeanUtils.copyProperties(bdsDeleteInputDTO, gzyqXtBds);
        gzyqXtBds.setZhxgsj(new Date());
        int i = gzyqXtBdsMapper.delete(gzyqXtBds);
        if (i != 1) {
            throw new AppException("删除表达式失败");
        }
        return true;
    }

    @Override
    public GzdyByGzbmQueryOutputDTO queryGzdyByGzbm(GzdyByGzbmQueryInputDTO xmListQueryInputDTO) {
        GzyqXtGzdy gzyqXtGzdy = new GzyqXtGzdy();
        BeanUtils.copyProperties(xmListQueryInputDTO, gzyqXtGzdy);
        gzyqXtGzdy = gzyqXtGzdyMapper.queryGzdyByGzbm(gzyqXtGzdy);
        if (gzyqXtGzdy == null) {
            return null;
        }
        GzdyByGzbmQueryOutputDTO gzdyByGzbmQueryOutputDTO = new GzdyByGzbmQueryOutputDTO();
        BeanUtils.copyProperties(gzyqXtGzdy, gzdyByGzbmQueryOutputDTO);
        return gzdyByGzbmQueryOutputDTO;
    }

    @Override
    public Gzjyjg testRule(RuleTestInputDTO ruleTestInputDTO) {
        List<FormDataObject> formDataObjectList = ruleTestInputDTO.getData();
        List<RuleInfo> ruleInfoList = ruleTestInputDTO.getRuleInfos();
        return RuleEngine.excute(formDataObjectList, ruleInfoList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean copyGz(GzCopyInputDTO gzCopyInputDTO) {
        GzyqXtGzdy gzyqXtGzdy = new GzyqXtGzdy();
        gzyqXtGzdy.setGzbm(gzCopyInputDTO.getGzbm());
        gzyqXtGzdy = gzyqXtGzdyMapper.queryGzdyByGzbm(gzyqXtGzdy);

        GzyqXtBds gzyqXtBds = new GzyqXtBds();
        gzyqXtBds.setGzbm(gzCopyInputDTO.getGzbm());
        List<BdsListByGzbmQueryOutputDTO> bdsListByGzbmList = gzyqXtBdsMapper.queryBdsListByGzbm(gzyqXtBds);

        GzdyAddInputDTO gzdyAddInputDTO = new GzdyAddInputDTO();
        BeanUtils.copyProperties(gzyqXtGzdy, gzdyAddInputDTO);
        gzdyAddInputDTO.setGzflbm(gzCopyInputDTO.getGzflbm());
        String gzbm = addGzdy(gzdyAddInputDTO);

        GzbdsUpdateInputDTO gzbdsUpdateInputDTO = new GzbdsUpdateInputDTO();
        BeanUtils.copyProperties(gzyqXtGzdy, gzbdsUpdateInputDTO);
        gzbdsUpdateInputDTO.setGzbm(gzbm);
        updateGzbds(gzbdsUpdateInputDTO);

        BdsAddInputDTO bdsAddInputDTO;
        for (BdsListByGzbmQueryOutputDTO bdsListByGzbmQueryOutputDTO : bdsListByGzbmList) {
            bdsAddInputDTO = new BdsAddInputDTO();
            BeanUtils.copyProperties(bdsListByGzbmQueryOutputDTO, bdsAddInputDTO);
            bdsAddInputDTO.setGzbm(gzbm);
            addBds(bdsAddInputDTO);
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean sortGzdy(List<GzdySortInputDTO> sortInputDtoList) {
        GzyqXtGzdy gzyqXtGzdy;
        for (GzdySortInputDTO gzdySortInputDTO : sortInputDtoList) {
            gzyqXtGzdy = new GzyqXtGzdy();
            BeanUtils.copyProperties(gzdySortInputDTO, gzyqXtGzdy);
            gzyqXtGzdy.setZhxgsj(new Date());
            gzyqXtGzdyMapper.sortGzdy(gzyqXtGzdy);
        }
        return true;
    }

    @Override
    public BdsByBdsbmQueryOutputDTO queryBdsByBdsbm(BdsByBdsbmQueryInputDTO bdsByBdsbmQueryInputDTO) {
        GzyqXtBds gzyqXtBds = new GzyqXtBds();
        BeanUtils.copyProperties(bdsByBdsbmQueryInputDTO, gzyqXtBds);
        gzyqXtBds = gzyqXtBdsMapper.queryBdsByBdsbm(gzyqXtBds);

        BdsByBdsbmQueryOutputDTO bdsByBdsbmQueryOutputDTO = new BdsByBdsbmQueryOutputDTO();
        BeanUtils.copyProperties(gzyqXtBds, bdsByBdsbmQueryOutputDTO);
        return bdsByBdsbmQueryOutputDTO;
    }

    @Override
    public List<GzinfoListByGjzQueryResult> queryGzinfoListByGjz(GzinfoListByGjzQueryParam gzinfoListByGjzQueryParam) {
        return gzyqXtGzdyMapper.queryGzinfoListByGjz(gzinfoListByGjzQueryParam);
    }

    @Override
    public int qkBds(BdsQkInputDTO qkbdsQkInputDTO) {
        List<String> gzbmList = qkbdsQkInputDTO.getGzbmList();
        if (gzbmList == null || gzbmList.isEmpty()) {
            return 0;
        }
        int count = 0;
        for (int i = 0; i < gzbmList.size(); i += 500) {
            int end = i + 500;
            if (end > gzbmList.size()) {
                end = gzbmList.size();
            }
            List<String> strings = gzbmList.subList(i, end);
            count += gzyqXtBdsMapper.qkBds(strings);
        }
        return count;
    }

    @Override
    public int qkGzdy(GzdyQkInputDTO qkgzdyQkInputDTO) {
        List<String> gzflbmList = qkgzdyQkInputDTO.getGzflbmList();
        if (gzflbmList == null || gzflbmList.isEmpty()) {
            return 0;
        }
        int count = 0;
        for (int i = 0; i < gzflbmList.size(); i += 500) {
            int end = i + 500;
            if (end > gzflbmList.size()) {
                end = gzflbmList.size();
            }
            List<String> strings = gzflbmList.subList(i, end);
            count += gzyqXtGzdyMapper.qkGzdy(strings);
        }
        return count;
    }

    @Override
    public int importGzdyList(List<GzDataDTO.Gzdy> gzdyList) {
        if (gzdyList == null || gzdyList.isEmpty()) {
            return 0;
        }
        int count = 0;
        for (int i = 0; i < gzdyList.size(); i += 500) {
            int end = i + 500;
            if (end > gzdyList.size()) {
                end = gzdyList.size();
            }
            List<GzDataDTO.Gzdy> strings = gzdyList.subList(i, end);
            count += gzyqXtGzdyMapper.importGzdyList(strings);
        }
        return count;
    }

    @Override
    public int importBdsList(List<GzDataDTO.Bds> bdsList) {
        if (bdsList == null || bdsList.isEmpty()) {
            return 0;
        }
        int count = 0;
        for (int i = 0; i < bdsList.size(); i += 500) {
            int end = i + 500;
            if (end > bdsList.size()) {
                end = bdsList.size();
            }
            List<GzDataDTO.Bds> strings = bdsList.subList(i, end);
            count += gzyqXtBdsMapper.importBdsList(strings);
        }
        return count;
    }
}
